/**
 *
 */
package com.angel.mocks.io.rowProcessorCommands;

import com.angel.architecture.daos.CountryDAO;
import com.angel.architecture.persistence.beans.Country;
import com.angel.common.helpers.StringHelper;
import com.angel.data.generator.annotations.Inject;
import com.angel.io.annotations.ColumnRow;
import com.angel.io.annotations.RowChecker;
import com.angel.io.annotations.RowProcessor;
import com.angel.io.annotations.RowProcessorCommand;
import com.angel.io.exceptions.InvalidRowDataException;

/**
 * @author William
 *
 */
@RowProcessorCommand
	(
		columnsRow = {
			@ColumnRow( columnName = CountryAnnotationRowProcessorCommand.CODE_COLUMN, aliases = {"Codigo de Pais"} ),
			@ColumnRow( columnName = CountryAnnotationRowProcessorCommand.NAME_COLUMN, aliases = {"Nombre del Pais"} ),
			@ColumnRow( columnName = CountryAnnotationRowProcessorCommand.SHORT_CODE_COLUMN, aliases = {"Codigo Corto"} ),
		}
	)
public class CountryAnnotationRowProcessorCommand {

	public static final String CODE_COLUMN = "code";
	public static final String NAME_COLUMN = "name";
	public static final String SHORT_CODE_COLUMN = "shortCode";

	@Inject
	private CountryDAO countryDAO;

    @RowChecker(columnsParameters = {})
    public void checkRowData(String code, String name, String shortCode) throws InvalidRowDataException {
		boolean areAllNotEmpty = StringHelper.areAllNotEmpty(name, code, shortCode);
		if(!areAllNotEmpty){
			throw new InvalidRowDataException("Some row data are NULL - " +
					"Name: [" + name + "] - " +
					"Code: [" + code + "] - " +
					"Short Code: [" + shortCode + "] - ");
		}
		Country country = this.getCountryDAO().findUniqueOrNullByName(name);
		if(country != null){
			throw new InvalidRowDataException("Country exist with name [" + name + "].");
		}
    }

	@RowProcessor(columnsParameters = {}, object = Country.class, inject = true)
	public Country processRow(Country country, String code, String name, String shortCode) {
		country.setCode(code);
		country.setName(name);
		country.setShortCode(shortCode);
        return country;
    }

	/**
	 * @return the countryDAO
	 */
	public CountryDAO getCountryDAO() {
		return countryDAO;
	}

	/**
	 * @param countryDAO the countryDAO to set
	 */
	public void setCountryDAO(CountryDAO countryDAO) {
		this.countryDAO = countryDAO;
	}
}
